<?php
/**
 * function:常用方法的一些封装
 * Created by PhpStorm.
 * User: zola
 * Date: 2016/05/10
 */

class RepairService extends BaseService
{

    public function _initialize()
    {
        parent::_initialize();

    }

    /**
     * 判断该维修项目下是否有子项目
     * @param $repairId
     * @param $projectId
     * @return array|bool
     */
    public function hasChildProject($repairId,$projectId) {
        $condition = array(
            'cut_off' => 0,
            'repair_id' => $repairId,
            'parent_id' => $projectId,
        );

        $data = M('RepairProject')->where($condition)->select();
        $projectIdArr = array();
        if($data) {
            foreach($data as $key => $value) {
                array_push($projectIdArr,$value['project_id']);
            }
            return $projectIdArr;
        } else {
            return false;
        }
    }

    /**
     * 获得报修一级分类
     */
    public function getRepairFirstCategory($repairId) {
        $condition = array(
            'parent_id' => 0,
            'cut_off' => 0,
            'repair_id' => $repairId,
        );
        $field = 'project_id,project_name';
        $projectCate = D('RepairProject')->getList($condition,$field,'sort');
        return $projectCate;
    }

    /**
     * 获得报修二级分类
     * @param $parentId
     *
     */
    public function getRepairSecondCategory($parentId,$repairId) {
        $mProject = D('RepairProject');
        $condition = array(
            'parent_id' => $parentId,
            'account_id' => $repairId,
            'cut_off' => 0,
        );
        $data = $mProject->getList($condition, '', 'sort asc');
        return $data;
    }

    /**
     * 获取订单最终得分和评价等级
     * @param $repairId
     * @param $orderData
     * @return array|bool
     */
    public function getOrderFinalScore($repairId,$orderData) {
        $returnData = array();
        $repairData = M('Repair')->where(array('repair_id' => $repairId))->find(); //该账号报修基本设置
        if($repairData) {
            $goodComment = explode(',',$repairData['good_star']);
            $middleComment = explode(',',$repairData['middle_star']);
            $badComment = explode(',',$repairData['bad_star']);
            $returnData['all_star'] = number_format($orderData['quality_star']*$repairData['response_star_ratio']/100 + $orderData['service_star']*$repairData['quality_star_ratio']/100 + $orderData['image_star']*$repairData['service_star_ratio']/100,2);

            if(in_array(intval($returnData[all_star]),$goodComment)) {
                $returnData['all_level'] = 1; //好评
            }elseif(in_array(intval($returnData['all_star']),$middleComment)) {
                $returnData['all_level'] = 2; //中评
            } elseif(in_array(intval($returnData['all_star']),$badComment)) {
                $returnData['all_level'] = 3; //差评
            }
            return $returnData;
        } else {
            return false;
        }
    }

}